package org.example.dao.impl;

import org.example.dao.StudentDao;
import org.example.entity.Student;
import org.example.entity.User;
import org.example.utils.DbUtil;
import org.example.vo.StudentVo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: StudentSystem
 * @BelongsPackage: org.example.dao.impl
 * @Author: StarBonny
 * @CreateTime: 2024-12-24  10:41
 * @Description: TODO
 * @Version: 1.0
 */
public class StudentDaoImpl implements StudentDao {
    @Override
    public List<Student> showStudent() {
        List<Student> list = new ArrayList<>();
        String SQL = "SELECT student_id,name,sex,age,phone,submit_date,status,class_id_fk FROM student";
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        Connection conn =  DbUtil.getConnection();
        try {
            pstmt = conn.prepareStatement(SQL);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Student stu = new Student();
                stu.setStudentId(rs.getInt("student_id"));
                stu.setName(rs.getString("name"));
                stu.setSex(rs.getString("sex"));
                stu.setAge(rs.getInt("age"));
                stu.setPhone(rs.getString("phone"));
                stu.setSubmitDate(rs.getDate("submit_date"));
                stu.setStatus(rs.getInt("status"));
                stu.setClassIdFk(rs.getInt("class_id_fk"));
                list.add(stu);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public List<StudentVo> showStudentClass() {
        List<StudentVo> list = new ArrayList<>();
        String SQL = "SELECT stu.name,stu.sex,stu.age,stu.phone,stu.submit_date,stu.status,cs.name,cs.submit_date,mj.name FROM student stu JOIN class cs ON stu.class_id_fk=cs.class_id JOIN major mj ON cs.major_id_fk=mj.major_id";
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        Connection conn =  DbUtil.getConnection();
        try {
            pstmt = conn.prepareStatement(SQL);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                StudentVo stu = new StudentVo();
                stu.setStudentName(rs.getString("name"));
                stu.setStudentSex(rs.getString("sex"));
                stu.setStudentAge(rs.getInt("age"));
                stu.setStudentPhone(rs.getString("phone"));
                stu.setStudentSubmitDate(rs.getDate("submit_date"));
                stu.setStudentStatus(rs.getInt("status"));
                stu.setClassName(rs.getString("name"));
                stu.setClassSubmitDate(rs.getDate("submit_date"));
                stu.setMajorName(rs.getString("name"));
                list.add(stu);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }
}
